package com.xuxueli.xxlrpc.registry.impl;

import com.xuxueli.xxlrpc.registry.Register;
import com.xuxueli.xxlrpc.registry.impl.xxlrpcadmin.XxlRpcAdminRegistryClient;
import com.xuxueli.xxlrpc.registry.impl.xxlrpcadmin.model.XxlRpcAdminRegistryDataParamVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2022/5/28 22:30
 */
public class XxlRpcAdminRegister extends Register {

    private static final Logger logger = LoggerFactory.getLogger(XxlRpcAdminRegister.class);

    public static final String ADMIN_ADDRESS = "ADMIN_ADDRESS";
    public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
    public static final String BIZ = "BIZ";
    public static final String ENV = "ENV";

    private XxlRpcAdminRegistryClient xxlRpcAdminRegistryClient;

    public XxlRpcAdminRegistryClient getXxlRpcAdminRegistryClient() {
        return xxlRpcAdminRegistryClient;
    }

    @Override
    public void start(Map<String, String> param) {
        logger.info("XxlAdminRegister.start");
        String xxlRegistyAddress = param.get(ADMIN_ADDRESS);
        String accessToken = param.get(ACCESS_TOKEN);
        String biz = param.get(BIZ);
        String env = param.get(ENV);
        biz = (biz != null && biz.trim().length() > 0) ? biz : "default";
        env = (env != null && env.trim().length() > 0) ? env : "default";
        xxlRpcAdminRegistryClient = new XxlRpcAdminRegistryClient(xxlRegistyAddress, accessToken, biz, env);
    }

    @Override
    public void stop() {


    }

    @Override
    public boolean registry(Set<String> keys, String value) {
        if (keys == null || keys.size() == 0 || value == null || value.trim().length() == 0) {
            return false;
        }
        // init
        List<XxlRpcAdminRegistryDataParamVO> registryDataList = new ArrayList<>();
        for (String key : keys) {
            registryDataList.add(new XxlRpcAdminRegistryDataParamVO(key, value));
        }

        logger.info("开始注册XxlRpcAdminRegister.registry");
        return xxlRpcAdminRegistryClient.registry(registryDataList);
    }

    @Override
    public boolean remove(Set<String> keys, String value) {
        return false;
    }

    @Override
    public Map<String, TreeSet<String>> discovery(Set<String> appkeys) {
        return xxlRpcAdminRegistryClient.discovery(appkeys);
    }

    @Override
    public TreeSet<String> discovery(String key) {
        return null;
    }
}
